package com.lintcode.solution;

import java.util.LinkedList;

public class Solution_12 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(1);
        System.out.println(minStack.pop());
        minStack.push(2);
        minStack.push(3);
        System.out.println(minStack.min());
        minStack.push(1);
        System.out.println(minStack.min());
        minStack.pop();
        minStack.pop();
        minStack.pop();
        System.out.println(minStack.min());

    }
}

class MinStack {

    private LinkedList<Integer> list;
    private int min;

    public MinStack() {
        list = new LinkedList<>();
        min = Integer.MAX_VALUE;
    }

    /*
     * @param number: An integer
     * @return: nothing
     */
    public void push(int number) {
        min = Math.min(min, number);
        list.push(number);
    }

    /*
     * @return: An integer
     */
    public int pop() {
        int pop = list.pop();
        if (pop == min) {
            min = Integer.MAX_VALUE;
            for (Integer integer : list) {
                min = Math.min(min, integer);
            }
        }
        return pop;
    }

    /*
     * @return: An integer
     */
    public int min() {
        if (list.size() == 0) {
            throw new RuntimeException("aaa");
        }
        return min;
    }
}
